package com.admin.kotlintest.adapter

import android.content.Context
import android.view.View
import android.widget.Button
import com.admin.kotlintest.R
import com.admin.kotlintest.base.AdapterHolder
import com.admin.kotlintest.base.CurrencyAdapter
import com.admin.kotlintest.bean.Appointment
import com.admin.kotlintest.config.FixedValue
import com.admin.kotlintest.config.MyApplication
import com.admin.kotlintest.utils.DataUtil
import com.socks.library.KLog

/**
 * Created by 吴昶 on 2018/4/20.
 */
class AppiontmentRecordAdapter(context: Context,layoutId:Int,data:MutableList<Appointment>?,var listener:AcceptRefuseListener):CurrencyAdapter<Appointment>(context,layoutId,data){

    var typeName=MyApplication.getSharedPreferencesString(FixedValue.TYPE_NAME,"")

    override fun convert(holder: AdapterHolder, item: Appointment, position: Int) {
        holder.setTextViewText(R.id.tv_record_car_number,item.carNumber)
        holder.setTextViewText(R.id.tv_record_car_driver,item.driverName)
        holder.setTextViewText(R.id.tv_record_car_user,item.userName)
        holder.setTextViewText(R.id.tv_record_car_why,item.appointRemarks)
        holder.setTextViewText(R.id.tv_record_car_time,DataUtil.dateYMDHm.format(item.appointStartTime)+" - "+DataUtil.dateYMDHm.format(item.appointEndTime))
        var stateBtn=holder.getButton(R.id.btn_record_state)
        var refuse=holder.getButton(R.id.btn_record_refuse)
        var state=item.appointState
        when(state){
            0->{
                stateBtn!!.setBackgroundResource(R.drawable.background_appiontment_record_state_0)
                when(typeName){
                    "司机"->{
                        stateBtn.text="签收"
                        refuse!!.visibility=View.VISIBLE
                        stateBtn.setOnClickListener { listener.AcceptListener(item,stateBtn,refuse) }
                        refuse.setOnClickListener { listener.RefuseListener(item,stateBtn,refuse) }
                    }
                    else->{
                        stateBtn.text="待签收"
                        refuse!!.visibility=View.GONE
                    }
                }
            }
            1->{
                stateBtn!!.setBackgroundResource(R.drawable.background_appiontment_record_state_1)
                stateBtn.text="已接受"
                refuse!!.visibility=View.GONE
            }
            2->{
                stateBtn!!.setBackgroundResource(R.drawable.background_appiontment_record_state_2)
                stateBtn.text="进行中"
                refuse!!.visibility=View.GONE
            }
            3->{
                stateBtn!!.setBackgroundResource(R.drawable.background_appiontment_record_state_3)
                stateBtn.text="已完成"
                refuse!!.visibility=View.GONE
            }
            4->{
                stateBtn!!.setBackgroundResource(R.drawable.background_appiontment_record_state_0)
                stateBtn.text="已拒绝"
                refuse!!.visibility=View.GONE
            }
        }

    }

    interface AcceptRefuseListener{
        fun AcceptListener(item: Appointment,accept:Button,refuse:Button)
        fun RefuseListener(item: Appointment,accept:Button,refuse:Button)
    }
}